package airport.user.action;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import airport.user.dao.UserDAO;
import airport.user.pojo.User;

import com.opensymphony.xwork2.ActionSupport;

public class FindPasswordAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private String email;
	private String from = "airport.traveler.app@gmail.com";
	private String password = "osucse5911";
	private String to = null;
	private String subject = "Your Password in AirportApp";
	private String body = "Your Password is: ";

	static Properties properties = new Properties();
	static {
		properties.put("mail.smtp.host", "smtp.gmail.com");
		properties.put("mail.smtp.socketFactory.port", "465");
		properties.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		properties.put("mail.smtp.auth", "true");
		properties.put("mail.smtp.port", "465");
	}

	public String execute() {
		String ret = "success";
		try {
			Session session = Session.getDefaultInstance(properties,
					new javax.mail.Authenticator() {
						protected PasswordAuthentication getPasswordAuthentication() {
							return new PasswordAuthentication(from, password);
						}
					});
			System.out.println("get password");

			UserDAO dao = new UserDAO();
			String password = dao.findPasswordByEmail(email);
			to = email;
			body = body + password;
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(from));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(to));
			message.setSubject(subject);
			message.setText(body);
			Transport.send(message);
		} catch (Exception e) {
			ret = "error";
			e.printStackTrace();
		}
		return ret;
	}

	public void validate() {
		// the input email should be in the DB, otherwise error message has to
		// be printed out in the lostpw.jsp

		UserDAO dao = new UserDAO();
		User user = dao.findUserByEmail(email);

		String em = user.getEmail();

		if (!email.equals(em)) {
			addFieldError("email", "No matches found for input email address.");
		}
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

}
